﻿Option Strict On
Option Explicit On

Partial Class private_secure_admin_AssociateRequest
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Dim personId As Int32

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim getpersonid As New GetPersonIdFromUserId()
        u = Membership.GetUser(User.Identity.Name)
        personId = getpersonid.GetPersonId(CType(u.ProviderUserKey, Guid))
    End Sub

    Protected Sub ObjectDataSource1_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles ObjectDataSource1.Selecting
        e.InputParameters("PersonId") = personId
    End Sub
    Protected Sub btnSend_Click(sender As Object, e As System.EventArgs) Handles btnSend.Click
        If CDbl(ddlEntityList.SelectedValue) = 0 Then

            Label1.Text = "Please select item from list."
        Else

            Label1.Text = String.Empty
            u = Membership.GetUser(User.Identity.Name)

            Dim newutility As New Utility
            Dim token As Guid = Guid.NewGuid()

            Dim sendersPersonidTA As New LogginsLogDSTableAdapters.PersonTableAdapter()
            Dim sendersPersonid As Integer
            Try
                sendersPersonid = sendersPersonidTA.GetData(CType(u.ProviderUserKey, Guid?)).FirstOrDefault().Id
            Catch ex As Exception
                Throw ex
            End Try

            Dim virtualurl As String = ("http://" + HttpContext.Current.Request.Url.Authority.ToString() + "/" + _
                HttpContext.Current.Request.Url.Segments(1).ToString() + "Account/AssociateValidate.aspx?invitation=").Replace("Account/Account/", "Account/")

            Dim strMailFrom As String = ConfigurationManager.AppSettings("smtpmailfrom").ToString()
            Dim strMailTo As String = ConfigurationManager.AppSettings("accountapprovalemail").ToString()

            Dim sendemail As New SendEmailViaGoogle()

            Dim strSubject As String = "Your Associate Login"
            Dim strMessage As String = "A login has been requested for you by your colleague to use our site. To use this login as an Associate please click the link supplied in this email. Please use the link before it expires in 7 days. Please note that it can only be used one time."

            'Make sure the email address is not empty
            If txtEmail.Text.Length > 0 Then

                Dim NewPersonAssociateRequestTA As New LogginsLogDSTableAdapters.usp_PersonAssociateRequestTableAdapter()

                Dim b As Integer
                Try

                    '1- Write the request
                    b = NewPersonAssociateRequestTA.Insert(sendersPersonid, Convert.ToInt32(Me.ddlEntityList.SelectedValue), txtEmail.Text, token, False, DateTime.Now, 7, Nothing, True)

                    '2- Sending email to the new user
                    sendemail.SendEmail(strMailFrom, txtEmail.Text, strSubject, strMessage + " Please click here. " + virtualurl + token.ToString())

                    strMessage = "User: " + u.UserName + Environment.NewLine + _
                                        "requested for: " + txtEmail.Text + Environment.NewLine + " " + strMessage + " " + virtualurl + token.ToString()

                    '3- Sending email to the administrator
                    sendemail.SendEmail(strMailFrom, strMailTo, strSubject, strMessage)

                    '4- Also send an email to the person making the Request
                    strSubject = "The login you requested " + strMailTo
                    strMessage = "The login you requested for your Associate [" + strMailTo + "] has been sent. Please remind them that the login should be used before it expires in 7 days."
                    strMessage = "User: " + u.UserName + Environment.NewLine + _
                            "requested for: " + txtEmail.Text + Environment.NewLine + " " + strMessage + " " + virtualurl + token.ToString()
                    sendemail.SendEmail(strMailFrom, strMailTo, strSubject, strMessage)

                    Try
                        NewGoogleCalendarEvent.CreateEvent(ConfigurationManager.AppSettings("GoogleCalendarUsername").ToString(), ConfigurationManager.AppSettings("GoogleCalendarPassword").ToString(), "Your new token is:[" & token.ToString() & "]", "test content", "1", "Test Place", "1", "1")
                    Catch ex As Exception
                        Throw ex
                    End Try


                    '5- All done, now send the user to their home page

                    AccRegistrationSuccessModalPopupExtender.Show()
                Catch ex As Exception

                End Try
            Else
                'lblMessage.Text = "Please Specify an Email Address"
            End If
        End If

    End Sub


    Protected Sub btnAccRegistrationClose_Click(sender As Object, e As System.EventArgs) Handles btnAccRegistrationClose.Click
        Response.Redirect("~/private/secure_members/default.aspx")
    End Sub
End Class
